#include <SRL/Engine/GraphScene.h>

namespace srl {

    GraphScene::GraphScene(int w, int h, int fovy, double nearD, double farD) {
        glEnable(GL_DEPTH_TEST);
        glDepthMask(GL_TRUE);
        glClearDepth(1.f);
        glClearColor(0.5,0.5,0.5,1);
        
        glEnable(GL_TEXTURE_2D);
        //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(fovy, (double)w/h, 0.1, 1000);
    }
    
    void GraphScene::Init() {
        currentCamera->Perspective();
        currentCamera->Look();
        
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
        
        
    }
    void GraphScene::Draw() {
        glBegin(GL_QUADS);
		// Front Face
		float angleA = (glToScreen( 1.0f, -1.0f,  1.0f) - glToScreen(-1.0f, -1.0f,  1.0f)).dot(glToScreen(-1.0f,  1.0f,  1.0f) - glToScreen(-1.0f, -1.0f,  1.0f));
		float angleB = (glToScreen( 1.0f,  1.0f,  1.0f) - glToScreen( 1.0f, -1.0f,  1.0f)).dot(glToScreen(-1.0f, -1.0f,  1.0f) - glToScreen( 1.0f, -1.0f,  1.0f));
		float angleC = (glToScreen(-1.0f,  1.0f,  1.0f) - glToScreen( 1.0f,  1.0f,  1.0f)).dot(glToScreen( 1.0f, -1.0f,  1.0f) - glToScreen( 1.0f,  1.0f,  1.0f));
		float angleD = (glToScreen(-1.0f, -1.0f,  1.0f) - glToScreen(-1.0f,  1.0f,  1.0f)).dot(glToScreen( 1.0f,  1.0f,  1.0f) - glToScreen(-1.0f,  1.0f,  1.0f));
		
		glNormal3f( 0.0f, 0.0f, 0.5f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		// Back Face
		glNormal3f( 0.0f, 0.0f,-0.5f);		
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		// Top Face
		glNormal3f( 0.0f, 0.5f, 0.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		// Bottom Face
		glNormal3f( 0.0f,-0.5f, 0.0f);					
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		// Right Face
		glNormal3f( 0.5f, 0.0f, 0.0f);					
		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
		// Left Face
		glNormal3f(-0.5f, 0.0f, 0.0f);					
		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
	glEnd();
        
        /*for(std::map<std::string, srl::Object>::iterator it = objects.begin(); it != objects.end(); it++) {
            it->second.Draw();
        }*/
    }
    
    /*srl::Object* GraphScene::AddObject(std::string id, std::string url) {
        objects[id].Open(url);
    }*/
    
    srl::Camera* GraphScene::AddCamera(std::string id, bool current, float p_visionField, float p_walkSpeed, float p_runSpeed, int p_turnSpeed) {
        cameras[id].Init(70, 800/600, 0.1, 1000, p_visionField,p_walkSpeed,p_runSpeed,p_turnSpeed);
        if(current)
            currentCamera = &cameras[id];
    }
    
}
